package com.example.RuanJianKeShe.Dao.impl;

import com.example.RuanJianKeShe.Bean.Student;
import com.example.RuanJianKeShe.Bean.Teacher;
import com.example.RuanJianKeShe.Dao.TeacherDao;
import com.example.RuanJianKeShe.Utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class TeacherDaoImpl implements TeacherDao {

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 获取所有教师信息
     *
     * @return List<Student>
     */
    @Override
    public List<Teacher> getAllTeacher() {
        String sql = "select * from teacher";
        try{
            List<Teacher> teacherList = template.query(sql,new BeanPropertyRowMapper<Teacher>(Teacher.class));
            return teacherList;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过教师号和密码获取教师信息
     *
     * @param id  int
     * @param key String
     * @return Teacher
     */
    @Override
    public Teacher getTeacherByIdAndKey(int id, String key) {
        String sql = "select * from teacher where tno = ? and teaKey = ?";
        try {
            Teacher teacher = template.queryForObject(sql,new BeanPropertyRowMapper<Teacher>(Teacher.class),id,key);
            return teacher;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过教师号查找教师
     *
     * @param id int
     * @return Teacher
     */
    @Override
    public Teacher getTeacherById(int id) {
        String sql = "select * from teacher where tno = ?";
        try{
            Teacher teacher = template.queryForObject(sql,new BeanPropertyRowMapper<Teacher>(Teacher.class),id);
            return teacher;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 通过教师号修改教师信息
     *
     * @param tea String
     * @return boolean
     */
    @Override
    public boolean setTeacherById(Teacher tea) {
        String sql = "update Teacher set tname=?,tage=?,tsex=?,telephone=?,email=?,teakey=?  where tno=? ";
        try {
            template.update(sql,tea.getTname(),tea.getTage(),tea.getTsex(),tea.getTelephone(),tea.getEmail(),tea.getTeakey(),tea.getTno());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }


        return false;
    }

    /**
     * 通过教师类来添加教师信息
     *
     * @param teacher Teacher
     * @return boolean
     */
    @Override
    public boolean addTeacher(Teacher teacher) {
        String sql = "insert into teacher(tname,tage,tsex,telephone,email,teakey,createTime) values(?,?,?,?,?,?,?)";
        try{
            template.update(sql,teacher.getTname(),teacher.getTage(),teacher.getTsex(),teacher.getTelephone(),
                    teacher.getEmail(),teacher.getTeakey(),teacher.getCreateTime());
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 通过教师号删除教师
     *
     * @param id int
     * @return 被删除的教师
     */
    @Override
    public boolean delTeacher(int id) {
        String sql = "delete from teacher where tno = ?";
        try{
            template.update(sql,id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
